﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using MyDBconnection;
using Cat;

namespace Thesis_2
{
    public partial class frmCategorylist : Form
    {
        DatabaseConnection dbconn = new DatabaseConnection();
      
        public frmCategorylist()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmCategorylist_Load);
            txtSearch.TextChanged+=new EventHandler(txtSearch_TextChanged);
            txtSearch.Click += new EventHandler(txtSearch_Click);
            dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
            dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
            linkLabel1.Click += new EventHandler(linkLabel1_Click);     
        }

        void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            string strRowNumber = (e.RowIndex + 1).ToString();
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
            if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 130)))
            {
                dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 130));
            }
            Brush b = SystemBrushes.ControlText;
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }
        void txtSearch_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }
        void linkLabel1_Click(object sender, EventArgs e)
        {
            frmCategory frmC = new frmCategory();
            frmC.Show();
            this.Close();
        }
        void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView1.SelectedRows[0];
            this.Hide();
            frmCategory frmC = new frmCategory();
            frmC.Show();
            txtSearch.Focus();
            frmC.txtCategoryName.Text = dr.Cells[1].Value.ToString();
            frmC.textBox1.Text = dr.Cells[1].Value.ToString();
            frmC.btnSave.Enabled = false;
            frmC.btnUpdate.Enabled = true;
        }
        void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text != "Search Category Name")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT  * FROM tblcategory WHERE CategoryName LIKE '%" + txtSearch.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {

                loadData();
            
            }
             
        }
        void frmCategorylist_Load(object sender, EventArgs e)
        {
            loadData();
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

        }
        void loadData()
        {
            try
            {
                dbconn.dbconnection();
                dbconn.loadproduct("Select *  from tblcategory");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        
        
        }
    }
}